home *** CD-ROM | disk | FTP | other *** search
- #include<math.h>
- /* Following is inline macro for drawing lines */
- #define viewpts(s) {for(i=0;i<numoflns;i++) \
- { MoveTo((int)vertex[s][line[i].v1].x, \
- (int)vertex[s][line[i].v1].y); \
- LineTo((int)vertex[s][line[i].v2].x, \
- (int)vertex[s][line[i].v2].y); }}
-
- #define numofpts 8 /* A cube has eight vertices */
- #define numoflns 12 /* lines for every face. */
-
- /* the following are the data structs for vertices and lines*/
- typedef struct rec1 {float x,y,z;} point3d;
- typedef struct rec2 {int v1,v2;} edge;
-
- void mult(); /* Matrices multiplication */
-
- main()
- {
- point3d vertex[2][8], /* array of 3D pts */
- center; /* centroid of cube */
- edge line[12]; /* array of lines */
- int buttondown=0, /* mousedwn flag(for prog end)*/
- keypressed=0, /* keydwn flg(for arrows) */
- flip=0, /* This is index for vertex so*/
- flop=1, /* can undraw flip & draw flop*/
- i, /* counter */
- rot=0; /* Flag for direction of rotat*/
- long low; /* low word of keydwn message */
- float a, /* Particular angle of rotat */
- R[4][4], /* Rotation matrix */
- c[4][4], /* Product of trans & rot mats*/
- d[4][4], /* Product of c and inv trans */
- T[4][4],Tinv[4][4], /* Translation & inv trans */
- x=0.087266; /* Algle of rot in rad */
- EventRecord nextevent;
- KeyMap thekeys;
- WindowPtr scnwdw;
- Rect scnrect;
- /*********************************************
- * Set things up *
- *********************************************/
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent,0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
- InitCursor();
- scnrect=screenBits.bounds;
- InsetRect(&scnrect,50,50);
- scnwdw=NewWindow(0,&scnrect,"\p",TRUE,dBoxProc,-1,FALSE,0);
-
- /*********************************************
- * Get points. Arbitrary cube. *
- *********************************************/
- center.x=300;center.y=200;center.z=120;
- vertex[0][0].x=280;vertex[0][0].y=220;vertex[0][0].z=100;
- vertex[0][1].x=320;vertex[0][1].y=220;vertex[0][1].z=100;
- vertex[0][2].x=320;vertex[0][2].y=180;vertex[0][2].z=100;
- vertex[0][3].x=280;vertex[0][3].y=180;vertex[0][3].z=100;
- vertex[0][4].x=280;vertex[0][4].y=220;vertex[0][4].z=140;
- vertex[0][5].x=320;vertex[0][5].y=220;vertex[0][5].z=140;
- vertex[0][6].x=320;vertex[0][6].y=180;vertex[0][6].z=140;
- vertex[0][7].x=280;vertex[0][7].y=180;vertex[0][7].z=140;
- line[0].v1=0;line[0].v2=1;
- line[1].v1=1;line[1].v2=2;
- line[2].v1=2;line[2].v2=3;
- line[3].v1=3;line[3].v2=0;
- line[4].v1=0;line[4].v2=4;
- line[5].v1=1;line[5].v2=5;
- line[6].v1=2;line[6].v2=6;
- line[7].v1=3;line[7].v2=7;
- line[8].v1=4;line[8].v2=5;
- line[9].v1=5;line[9].v2=6;
- line[10].v1=6;line[10].v2=7;
- line[11].v1=7;line[11].v2=4;
- T[0][0]=1;T[0][1]=0;T[0][2]=0;T[0][3]=0;
- T[1][0]=0;T[1][1]=1;T[1][2]=0;T[1][3]=0;
- T[2][0]=0;T[2][1]=0;T[2][2]=1;T[2][3]=0;
- T[3][0]=-center.x;T[3][1]=-center.y;T[3][2]=-center.z;T[3][3]=1;
- Tinv[0][0]=1;Tinv[0][1]=0;Tinv[0][2]=0;Tinv[0][3]=0;
- Tinv[1][0]=0;Tinv[1][1]=1;Tinv[1][2]=0;Tinv[1][3]=0;
- Tinv[2][0]=0;Tinv[2][1]=0;Tinv[2][2]=1;Tinv[2][3]=0;
- Tinv[3][0]=center.x;Tinv[3][1]=center.y;Tinv[3][2]=center.z;Tinv[3][3]=1;
-
- /*********************************************
- * Rotate *
- *********************************************/
- viewpts(flip); /* This draws first set of pts*/
- while(!buttondown) /* Mini event loop */
- {
- keypressed=0;
- SystemTask();
- if(GetNextEvent(-1,&nextevent))
- if(nextevent.what==mouseDown) buttondown=1;
- else if(nextevent.what==keyDown) keypressed=1;
- else if(nextevent.what==autoKey) keypressed=1;
- if(keypressed) /* Find out which one */
- {
- keypressed=0;
- low=LoWord(nextevent.message);
- low=BitShift(low,-8);
- if(low==126) {rot=1;a=-x;} /* Set dir flag and-*/
- if(low==124) {rot=2;a=-x;} /* angle(pos or neg */
- if(low==125) {rot=3;a=x;}
- if(low==123) {rot=4;a=x;}
- switch(rot)
- {
- case 1:/* Both of these are rot about the X axis */
- case 3: R[0][0]=1;R[0][1]=0;R[0][2]=0;R[0][3]=0;
- R[1][0]=0;R[1][1]=cos(a);R[1][2]=sin(a);R[1][3]=0;
- R[2][0]=0;R[2][1]=-sin(a);R[2][2]=cos(a);R[2][3]=0;
- R[3][0]=0;R[3][1]=0;R[3][2]=0;R[3][3]=1;break;
- case 2:/* Both of these are rot about the Y axis */
- case 4: R[0][0]=cos(a);R[0][1]=0;R[0][2]=-sin(a);R[0][3]=0;
- R[1][0]=0;R[1][1]=1;R[1][2]=0;R[1][3]=0;
- R[2][0]=sin(a);R[2][1]=0;R[2][2]=cos(a);R[2][3]=0;
- R[3][0]=0;R[3][1]=0;R[3][2]=0;R[3][3]=1;break;
- } /*end switch*/
- mult(T,R,c); /* Combine trans & rotation */
- mult(c,Tinv,d); /* Combine that and inv trans */
- flip++;flip=flip%2;flop++;flop=flop%2; /* flip flop */
- /* The following actually calculates new vert of rotat*/
- for(i=0;i<numofpts;i++)
- {
- vertex[flip][i].x=vertex[flop][i].x*d[0][0]
- +vertex[flop][i].y*d[1][0]
- +vertex[flop][i].z*d[2][0]
- +1*d[3][0];
- vertex[flip][i].y=vertex[flop][i].x*d[0][1]
- +vertex[flop][i].y*d[1][1]
- +vertex[flop][i].z*d[2][1]
- +1*d[3][1];
- vertex[flip][i].z=vertex[flop][i].x*d[0][2]
- +vertex[flop][i].y*d[1][2]
- +vertex[flop][i].z*d[2][2]
- +1*d[3][2];
- }
- ForeColor(whiteColor);
- viewpts(flop); /* Undraw */
- ForeColor(blackColor);
- viewpts(flip); /* Draw */
- } /*end update points*/
-
- }
-
- /*********************************************
- * End everything *
- *********************************************/
- DisposeWindow(scnwdw);
- } /*program end*/
-
-
- void mult(A,B,C)
- float A[][4],B[][4],C[][4];
- {
- int i,j,k;
-
- for(i=0;i<=3;i++)
- for(j=0;j<=3;j++)
- {
- C[i][j]=0.0;
- for(k=0;k<=3;k++)
- C[i][j]+=A[i][k]*B[k][j];
- }
- } /*end mult*/
-
-
-
-
-
-
-
-
-